package pl.edu.agh.to1.dice.logic;

public class PlayerResult{
	private int points[];
	private int bonus;
	private int suma[];

	public PlayerResult(){
		suma = new int[2];
		bonus = 0;
		suma[0] = 0;
		suma[1] = 0;
		points = new int[13];
		for(int i=0; i < points.length; i++)
			points[i] = -1;
	}

	private void setSingle(int pos, int states[]){
		points[pos] = 0;
		for(int b : states){
			if(b == pos+1)
				points[pos] += b;
		}
	}

	private void setCombo(int pos, int states[]){
		points[pos+3] = 0;
		int kosci[] = new int[6];
		int sum=0;
		for(int i=0;i<6;i++){
			kosci[i] = 0;
		}
		for(int b : states){
			kosci[b-1]++;
			sum += b;
		}
		for(int i=0;i<6;i++){
			if(kosci[i] >= pos)
				points[pos+3] = sum;
		}
	}

	private void setFul(int states[]){
		points[8] = 0;
		int kosci[] = new int[6];
		for(int i=0;i<6;i++){
			kosci[i] = 0;
		}
		for(int b : states){
			kosci[b-1]++;
		}
		boolean th=false,fo=false;
		for(int i=0;i<6;i++){
			if(kosci[i] == 2)
				th = true;
			if(kosci[i] == 3)
				fo = true;			
		}
		if(th == true && fo == true)
			points[8]=25;
	}

	private void setStrit(int len, int states[]){
		points[len+6] = 0;
		int kosci[] = new int[6];
		for(int i=0;i<6;i++){
			kosci[i] = 0;
		}
		for(int b : states){
			kosci[b-1]++;
		}
		boolean th=false;
		int t=0;
		for(int i=0;i<6;i++){
			if(kosci[i]> 0)
				t++;				
			else
				t = 0;		
			if(t == len)
				th=true;
		}
		if(th == true)
			points[len+6]=10*len;
	}

	private void setGeneral(int states[]){
		points[11] = 0;
		int kosci[] = new int[6];
		for(int i=0;i<6;i++){
			kosci[i] = 0;
		}
		for(int b : states){
			kosci[b-1]++;
		}
		for(int i=0;i<6;i++){
			if(kosci[i] == 5)
				points[11] = 50;
		}
	}

	public void updateResult(int pos, Dices dices){
		int states[] = dices.getStates();
		if(pos <= 5){
			setSingle(pos,states);
		}		
		if(pos == 6 || pos == 7){
			setCombo(pos-3,states);
		}
		if(pos == 8){
			setFul(states);
		}
		if(pos == 10 || pos == 9){
			setStrit(pos-6,states);
		}
		if(pos == 11){
			setGeneral(states);
		}
		if(pos == 12){
			points[pos] = 0;
			for(int b : states)
				points[pos] += b;
		}

		suma[0] = 0;
		suma[1] = 0;
		bonus = 0;
		for(int i=0;i<13;i++)
		{
			if( points[i] > 0){
				if( i < 6)				
					suma[0] += points[i];
				suma[1] += points[i];			
			}	
		}
		if(suma[0] > 63){
			bonus = 35;
			suma[0] += bonus;
			suma[1] += bonus;		
		}
	}

	public boolean isFree(int idk){
		return (points[idk] < 0);
	}

	public int getResult(int arg){
		return points[arg];
	}

	public int getSum(int part){
		if(part == 0)
			return suma[0];
		else if(part == 1)
			return suma[1];
		return bonus;	
	}

	public static String getName(int arg){
		switch(arg){
			case 0: 
				return "jedynki";
			case 1:
				return "dwojki";
			case 2:
				return "trojki";
			case 3:
				return "czworki";
			case 4:
				return "piatki";
			case 5:
				return "szostki";
			case 6:
				return "3-jednakowe";
			case 7:
				return "4-jednakowe";
			case 8:
				return "ful";
			case 9:
				return "maly strit";
			case 10:
				return "duzy strit";
			case 11:
				return "general";
			case 12:
				return "szansa";
			default:
				return "null";
		}
	}
}
